﻿var prm = null;
var activeUpdatePanel = null;
var panelToBeRefreshedId = null;
var postBackElementId = null;
var potentielId = null;

function onUpdatingPanel(sender, args) {
    if (args._updatePanelsToUpdate != null) {
        if (args._updatePanelsToUpdate.length == 1) {
            panelToBeRefreshedId = args._updatePanelsToUpdate[0];
        }
        var array = panelToBeRefreshedId.split('$');
        activeUpdatePanel = array[array.length - 1];
        array.splice(0, 1);
        panelToBeRefreshedId = array.join('_');
        panelToBeRefreshedId = '#' + panelToBeRefreshedId;
        var postBackElementArray = args._postBackElement.id.split('_');
        postBackElementId = postBackElementArray[postBackElementArray.length - 2];
        postBackElementArray[postBackElementArray.length - 2] = activeUpdatePanel;
        potentielId = '#' + postBackElementArray.join('_');
        switch (activeUpdatePanel) {
            case "updatePanelEditParagraph":
                {
                    if (postBackElementId == 'btnDeleteParagraph') {
                        onDeletingParagraph(potentielId);
                        //onDeletingParagraph(panelToBeRefreshedId);
                    }
                    else {
                        onUpdatingParagraph(potentielId);
                        //onUpdatingParagraph(panelToBeRefreshedId);
                    }
                    break;
                };
            case "updatePanelAddParagraph":
                {
                    onAddingParagraph(potentielId);
                    //onAddingParagraph(panelToBeRefreshedId);
                    break;
                };
            case "updatePanelImgDesc":
                {
                    onUpdatingImageDetails(potentielId);
                    //onUpdatingImageDetails(panelToBeRefreshedId);
                    break;
                }
        }
    }
}

function onUpdatedPanel(sender, args) {
    switch(activeUpdatePanel){
        case "updatePanelEditParagraph":
            {
                if (postBackElementId == 'btnDeleteParagraph') {
                    onDeletedParagraph(potentielId)
                }
                else {
                    onUpdatedParagraph(potentielId);
                }
                break;
            };
        case "updatePanelAddParagraph":
            {
                onAddedParagraph(potentielId);
                break;
            };
        case "updatePanelAddImg":
            {
                onSelectedImage(potentielId);
                break;
            }
        case "updatepanelPagingDDLImages":
            {
                onPagedDDLImages(potentielId);
                break;
            }
        case "updatePanelImgDesc":
            {
                onUpdatedImageDetails($(potentielId));
                break;
            }
    }
}
function pageLoaded(sender, args) {
    var updatedPanels = args.get_panelsUpdated();
    var updatedPanel = null;
    var array = null;
    var size = 0;
    if (updatedPanels.length == 1) {
        updatedPanel = updatedPanels[0];
        if (activeUpdatePanel == null) {
            array = updatedPanel.id.split('_');
            size = array.length;
            activeUpdatePanel = array[size - 1];
        }
        if (potentielId == null) {
            potentielId = updatedPanel;
        }
    }
}

$(function () {
    $(document).ready(function () {
        prm = Sys.WebForms.PageRequestManager.getInstance();
        // Hook up Page Event Handler - called when panels are created/updated
        prm.add_pageLoaded(pageLoaded);
        //On updating
        prm.add_beginRequest(onUpdatingPanel);
        //On updated
        prm.add_endRequest(onUpdatedPanel);
    });
});
